package com.mapaower.project.controller;

import com.mapaower.project.entity.Project;
import com.mapaower.project.service.ProjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Optional;

/**
 * 项目控制器
 * 处理项目相关的HTTP请求
 */
@RestController
@RequestMapping("/api/projects")
public class ProjectController {

    @Autowired
    private ProjectService projectService;

    /**
     * 创建新项目
     */
    @PostMapping
    public ResponseEntity<Project> createProject(@RequestBody Project project) {
        Project createdProject = projectService.createProject(project);
        return new ResponseEntity<>(createdProject, HttpStatus.CREATED);
    }

    /**
     * 更新项目信息
     */
    @PutMapping("/{id}")
    public ResponseEntity<Project> updateProject(@PathVariable Long id, @RequestBody Project project) {
        project.setId(id);
        Project updatedProject = projectService.updateProject(project);
        return ResponseEntity.ok(updatedProject);
    }

    /**
     * 根据ID获取项目详情
     */
    @GetMapping("/{id}")
    public ResponseEntity<Project> getProjectById(@PathVariable Long id) {
        Optional<Project> project = projectService.getProjectById(id);
        return project.map(ResponseEntity::ok)
                .orElseGet(() -> ResponseEntity.notFound().build());
    }

    /**
     * 获取所有开放的项目
     */
    @GetMapping("/open")
    public ResponseEntity<List<Project>> getOpenProjects() {
        List<Project> projects = projectService.getOpenProjects();
        return ResponseEntity.ok(projects);
    }

    /**
     * 根据创建者ID获取项目列表
     */
    @GetMapping("/creator/{creatorId}")
    public ResponseEntity<List<Project>> getProjectsByCreatorId(@PathVariable Long creatorId) {
        List<Project> projects = projectService.getProjectsByCreatorId(creatorId);
        return ResponseEntity.ok(projects);
    }

    /**
     * 根据类型和状态获取项目
     */
    @GetMapping("/type/{type}/status/{status}")
    public ResponseEntity<List<Project>> getProjectsByTypeAndStatus(
            @PathVariable Project.ProjectType type,
            @PathVariable Project.ProjectStatus status) {
        List<Project> projects = projectService.getProjectsByTypeAndStatus(type, status);
        return ResponseEntity.ok(projects);
    }

    /**
     * 搜索项目
     */
    @GetMapping("/search")
    public ResponseEntity<List<Project>> searchProjects(@RequestParam String keyword) {
        List<Project> projects = projectService.searchProjects(keyword);
        return ResponseEntity.ok(projects);
    }

    /**
     * 更新项目状态
     */
    @PutMapping("/{id}/status")
    public ResponseEntity<Project> updateProjectStatus(@PathVariable Long id, @RequestParam Project.ProjectStatus status) {
        Project updatedProject = projectService.updateProjectStatus(id, status);
        return ResponseEntity.ok(updatedProject);
    }

    /**
     * 删除项目
     */
    @DeleteMapping("/{id}")
    public ResponseEntity<Void> deleteProject(@PathVariable Long id) {
        projectService.deleteProject(id);
        return ResponseEntity.noContent().build();
    }
}